package com.lianzt.util;

public class CheckDataUtil {
	public static final int CHECK_LENGTH_LESS = 0;
	public static final int CHECK_LENGTH_SURE = 1;

	/**
	 * 
	 * @param obj
	 *            要检验信息名称
	 * @param msg
	 *            要检验信息
	 * @param len
	 *            检验长度
	 * @param mode
	 *            模式
	 * @return
	 */
	public static String checkStringLength(String obj, String msg, int len,
			int mode) {
		obj=obj.trim();
		msg=obj.trim();
		String back_msg = "-1";
		if (mode == CHECK_LENGTH_LESS) {
			if (msg.length() > len) {
				back_msg = obj + "：长度不能大于" + len;
			}
		} else if (mode == CHECK_LENGTH_SURE) {
			if (msg.length() != len) {
				back_msg = obj + "：长度必须是" + len;
			}
		}
		return back_msg;
	}

	public static String checkStringIsNum(String obj, String msg) {
		String back_msg = "-1";
		obj=obj.trim();
		msg=obj.trim();
		boolean flag = true;
		char[] c = msg.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (!Character.isDigit(c[i])) {
				flag = false;
				break;
			}
		}
		if (!flag) {
			back_msg = obj + "：内容必须是数字";
		}
		return back_msg;
	}

	public static String checkBirthday(String birthday, int identityType) {
		String back_msg = "-1";
		birthday=birthday.trim();
		if (isEmpty(birthday)) {
			back_msg = "出生日期不能为空";
			return back_msg;
		}
		String birth1 = "((\\d{2})(0[123456789]|1[012])(0[1-9]|[12]\\d|30|31))";// 890718
		// 89718
		// yymmdd
		String birth2 = "(((1[6-9]|[2-9]\\d)\\d{2})(0[123456789]|1[012])(0[1-9]|[12]\\d|30|31))";// 19890621
		if (identityType == 0) {
			if (!birthday.matches(birth1)) {
				back_msg = "身份证号中 出生日期必须是yymmdd格式且是合法的(1600年开始)";
			}
		}
		if (identityType == 1) {
			if (!birthday.matches(birth2)) {
				back_msg = "身份证号中出生日期必须是yyyymmdd格式且是合法的(1600年开始)";
			}
		}
		return back_msg;
	}

	public static String checkBirthday(String birthday) {
		String back_msg = "-1";
		birthday = birthday.trim();
		if (isEmpty(birthday)) {
			back_msg = "出生日期不能为空";
			return back_msg;
		}
		String birth2 = "(((1[6-9]|[2-9]\\d)\\d{2})(0[123456789]|1[012])(0[1-9]|[12]\\d|30|31))";// 19890621

		if (!birthday.matches(birth2)) {
			back_msg = "出生日期必须是yyyymmdd格式且是合法的(1600年开始)";
		}
		return back_msg;
	}

	public static String checkIdentityCard(String identityValue,
			int identityType) {
		String back_msg = "-1";
		if (isEmpty(identityValue)) {
			back_msg = "证件号码不能为空";
			return back_msg;
		}
		char last = identityValue.charAt(identityValue.length() - 1);
		if (identityType == 0) {
			if (identityValue.length() != 15) {
				back_msg = "身份证号码必须是15位";
				return back_msg;
			}
			String str = identityValue.substring(6, 12);
			System.out.println("str1:" + str);
			System.out.println("1 gener,match success return -1 :"
					+ checkBirthday(str, 0));
			
			if (!"-1".equals(checkBirthday(str, 0))) {
				return checkBirthday(str, 0);
			}
			if (!Character.isDigit(last) || identityValue.length() != 15) {
				back_msg = "身份证号的最后一位必须是数字或X";
			}
		}

		if (identityType == 1) {
			if (identityValue.length() != 18) {
				back_msg = "身份证号码的位数必须是18位";
				return back_msg;
			}
			String str = identityValue.substring(6, 14);
			System.out.println("str2:" + str);
			System.out.println("2 gener,match success return -1:"
					+ checkBirthday(str, 1));

			

			
			if (!"-1".equals(checkBirthday(str, 1))) {
				return checkBirthday(str, 1);
			}
			if (!(Character.isDigit(last) || last == 'X')) {
				back_msg = "身份证号 的最后一位必须是数字或者X";
			}
		}

		return back_msg;
	}

	public static String checkEmail(String email) {
		String back_msg = "-1";
		if (isEmpty(email)) {
			back_msg = "邮箱地址不能为空";
			return back_msg;
		}
		String emailFormat = "^([a-zA-Z0-9_\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";
		if (!email.matches(emailFormat)) {
			back_msg = "邮件的格式不正确(正确格式为xx@xx.xx)或者包含除字母、数字，下划线、中划线以外的非法字符";
		}
		return back_msg;
	}

	public static String checkPostCode(String postCode) {
		String back_msg = "-1";
		if (isEmpty(postCode)) {
			back_msg = "邮政编码不能为空";
			return back_msg;
		}
		String pCodeFormat = "[1-9]\\d{5}(?!\\d)";
		if (!postCode.matches(pCodeFormat)) {
			back_msg = "邮政编码格式不正确";
		}
		return back_msg;
	}

	public static boolean isEmpty(String object) {
		boolean flag = false;
		if (object == null || object.equals("")) {
			flag = true;
		}
		return flag;
	}
}
